package com.linkage.seapeak.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import butterknife.ButterKnife;


public abstract class CommonBaseFragment extends BaseFragment {

    protected Context          appContext;
    protected FragmentActivity mActivity;

    protected LayoutInflater inflater;
    private   ViewGroup      container;
    public    View           contentView;


    public Context getApplicationContext() {
        return appContext;
    }


    @Override
    public void onCreate( @Nullable Bundle savedInstanceState ) {
        //        LogUtil.d("--onCreate ---Current Fragment ==" + this);
        super.onCreate(savedInstanceState);
        appContext = getActivity().getApplicationContext();
        mActivity = getActivity();
    }

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        this.inflater = inflater;
        this.container = container;
        // Inflate the layout for this fragment
        initVariable(savedInstanceState);
        if (contentView == null) {
            onCreateView(savedInstanceState);
        }
        return contentView;
    }

    @Override
    public Context getContext() {
        if (super.getContext() == null) {
            return mActivity;
        }
        return super.getContext();
    }

    public void setContentView( int layoutResID ) {
        if (contentView == null || contentView instanceof FrameLayout) {
            setContentView(inflater.inflate(layoutResID, container, false));
        }
    }

    public View getContentView() {
        return contentView;
    }

    public void setContentView( View view ) {
        contentView = view;
    }

    protected void onCreateView( Bundle savedInstanceState ) {
        setContentView(getLayoutId());
        ButterKnife.bind(this, getContentView());
        initVariable(savedInstanceState);
        initViews();
        initData();
    }

    public View findViewById( int id ) {
        if (contentView != null)
            return contentView.findViewById(id);
        return null;
    }

    @Override
    public void onActivityCreated( @Nullable Bundle savedInstanceState ) {
        super.onActivityCreated(savedInstanceState);
    }

    protected void initVariable( Bundle savedInstanceState ) {
    }

    public void initViews() {

    }

    public void initData() {
    }

    protected abstract int getLayoutId();

}
